package com.demo.Mapper;

import com.demo.DemoApplicationTests;
import com.demo.mapper.SysUserModules.SysUserMapper;
import com.demo.mapper.nengxiaoyujing.nengxiaoyujingMapper;
import com.demo.model.Role.Role;
import com.demo.model.SysUser.SysUser;
import com.demo.model.nengxiaoyujing.nengxiaoyujing;
import com.demo.tool.Base64Strategy;
import com.demo.tool.JWTUTIL;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class UserMapper extends DemoApplicationTests {

    @Autowired
   private SysUserMapper userMapper;

    @Autowired
    private Base64Strategy strategy;

    @Autowired
    private nengxiaoyujingMapper mapper;


    @Test
    public void test(){
        SysUser user=userMapper.getUserByName("测试");
        System.out.println(user);
        int i=userMapper.countPhone("123456789");
        System.out.println(i);
        int j=userMapper.countName("测");
        System.out.println(j);
    }

    @Test
    public void add() throws UnsupportedEncodingException {
       SysUser user=new SysUser();
       user.setUsername("测试添加2");
       String password=strategy.encode("123456");
       user.setPassword(password);
       user.setPhone("14523664");
      int i= userMapper.save(user);
        System.out.println(i);
    }


    @Test
    public void test2(){
        System.out.println(System.currentTimeMillis());
        long time=15*60*1000;
        Date date=new Date(System.currentTimeMillis()+time);
        System.out.println(date);
        System.out.println(JWTUTIL.getToken(1L,"测试"));
        String token=JWTUTIL.getToken(1L,"测试");
        String token2= String.valueOf(UUID.randomUUID());

//        JWTUTIL.verify(token2);
//        System.out.println("解析后的token"+JWTUTIL.tokenInfo(token));
        System.out.println(JWTUTIL.verify(token));
    }

    @Test
    public void passwordjm(){
        BCryptPasswordEncoder cryptPasswordEncoder=new BCryptPasswordEncoder();
        String i=cryptPasswordEncoder.encode("123456");
        String i2=cryptPasswordEncoder.encode("123456");
        String i3=cryptPasswordEncoder.encode("1234567");
        System.out.println(i+":"+i2+":"+i3);
       Boolean result= cryptPasswordEncoder.matches("123456",i3);
        System.out.println(result);
    }

    //添加用户
    @Test
    public void adduser(){
        BCryptPasswordEncoder pas=new BCryptPasswordEncoder();
        SysUser user=userMapper.getUserByName("myLi6");
        System.out.println(user);
        System.out.println(user.getUid());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getAddress());
        System.out.println(user.getPhone());

        if(user.getPassword()!=null){
            user.setPassword(pas.encode("123456"));
        }
        String pass=user.getPassword();
        if(pas.matches("123456",pass)){
            if( userMapper.update(user)>0){
                System.out.println("正确");
            }
        }
    }


    @Test
    public void updateimg(){
        SysUser sysUser=new SysUser();
        sysUser.setUid(1L);
        sysUser.setHeadSculpture("http://localhost:8009/img/6482b463-fafd-4e69-a563-a419fd9519ca.png");
        Integer i= userMapper.update(sysUser);
        System.out.println(i);
    }

    @Test
    public void getRoles(){
        List<Role> roles=userMapper.getRolesByUid(7L);
        roles.forEach(System.out::println);
    }

    @Test
    public void getAll(){
        List<nengxiaoyujing> nengxiaoyujings=mapper.all();
        System.out.println(nengxiaoyujings);
    }
}
